#ifndef ChangeHUE
#define ChangeHUE

int current_hue=-1;

typedef struct {
  uint16_t h;   // 0-360
  uint8_t  s;   // 0-100
  uint8_t  v;   // 0-255
}hsv_t;


void RGB565ToHSV(uint16_t rgb565, hsv_t* hsv) {
  // 1. 拆出 5/6/5 位
  uint8_t r5 = (rgb565 >> 11) & 0x1F;
  uint8_t g6 = (rgb565 >> 5)  & 0x3F;
  uint8_t b5 =  rgb565        & 0x1F;

  // 2. 扩展到 8 bit
  uint8_t r = (r5 << 3) | (r5 >> 2);
  uint8_t g = (g6 << 2) | (g6 >> 4);
  uint8_t b = (b5 << 3) | (b5 >> 2);

  // 3. 计算 max/min/delta
  uint8_t max = r;
  if (g > max) max = g;
  if (b > max) max = b;

  uint8_t min = r;
  if (g < min) min = g;
  if (b < min) min = b;

  uint8_t delta = max - min;

  // 4. 计算 V
  hsv->v = max;

  // 5. 计算 S
  if (max == 0) {
    hsv->s = 0;
  } else {
    hsv->s = (uint32_t)delta * 100 / max;
  }

  // 6. 计算 H
  if (delta == 0) {
    hsv->h = 0;
  } else {
    int16_t h;
    if (max == r) {
      h = 60 * ((int16_t)(g - b) * 60) / delta;
      if (g < b) h += 360 * 60;
    } else if (max == g) {
      h = 120 * 60 + 60 * ((int16_t)(b - r) * 60) / delta;
    } else {
      h = 240 * 60 + 60 * ((int16_t)(r - g) * 60) / delta;
    }
    hsv->h = h / 60;      // 0-360
  }
}

uint16_t HSVToRGB565(uint16_t h, uint8_t s, uint8_t v) {
  // 1. 归一化
  uint8_t region = h / 60;
  uint16_t remainder = (h % 60) * 6;   // 0-360 → 0-359

  uint8_t p = ((uint32_t)v * (100 - s)) / 100;
  uint8_t q = ((uint32_t)v * (6000 - (uint32_t)s * remainder)) / 6000;
  uint8_t t = ((uint32_t)v * (6000 - (uint32_t)s * (60*6 - remainder))) / 6000;

  uint8_t r8, g8, b8;

  switch (region) {
    case 0: r8 = v; g8 = t; b8 = p; break;
    case 1: r8 = q; g8 = v; b8 = p; break;
    case 2: r8 = p; g8 = v; b8 = t; break;
    case 3: r8 = p; g8 = q; b8 = v; break;
    case 4: r8 = t; g8 = p; b8 = v; break;
    case 5:
    default: r8 = v; g8 = p; b8 = q; break;
  }

  // 2. 8 bit → 5/6/5
  uint16_t r5 = r8 >> 3;
  uint16_t g6 = g8 >> 2;
  uint16_t b5 = b8 >> 3;

  return (r5 << 11) | (g6 << 5) | b5;
}


uint16_t swapRB(uint16_t color) {
    // 首先，我们需要分别提取R、G、B分量
    uint16_t r = (color >> 11) & 0x1F; // 提取红色分量
    uint16_t g = (color >> 5) & 0x3F;  // 提取绿色分量
    uint16_t b = color & 0x1F;         // 提取蓝色分量

    // 然后，交换R和B分量
    uint16_t rb = r;
    r = b;
    b = rb;

    // 最后，重新组合R、G、B分量
    color = (r << 11) | (g << 5) | b;
    return color;
}

uint16_t tmpColor;
#define SWAP(x,y) tmpColor=x;x=y;y=tmpColor;

uint16_t changeHuepixel(uint16_t color){
//更改色调，有问题，先不用
  //rgb565->获取HSV
  //hsv_t hsv;
  //RGB565ToHSV(color,&hsv);
  //替换H
  //if(current_hue>=0)
  //  hsv.h = current_hue;
  //转成rgb565
  //return HSVToRGB565(hsv.h,hsv.s,hsv.v);

//----更改颜色-----根据当前的颜色模式 （current_hue) 
//0:默认不做改变
//1:交换蓝红
//2:交换蓝绿
//3：蓝+红
//4: 蓝+绿
//5: 红+绿；蓝=0
  // 首先，我们需要分别提取R、G、B分量
    uint16_t r = (color >> 11) & 0x1F; // 提取红色分量
    uint16_t g = (color >> 5) & 0x3F;  // 提取绿色分量
    uint16_t b = color & 0x1F;         // 提取蓝色分量
    
    current_hue=current_hue%6;
    // 然后，交换R和B分量
    switch (current_hue){
      case 1:
        SWAP(r,b);break;
      case 2:
        SWAP(r,g);break;
      case 3:
        r=b;break;
      case 4:
        g=b;break;
      case 5:
        r=b;g=b;b=0;break;  
    }
    
    // 最后，重新组合R、G、B分量
    color = (r << 11) | (g << 5) | b;
    return color;
}

void changeHue(uint16_t* bitmap,uint16_t w,uint16_t h){
  for(int i=0;i<w*h;i++){
    uint16_t color = bitmap[i];
    bitmap[i] = changeHuepixel(color);
  }
}

#endif